
using System;
using System.Collections.Generic;
using System.Linq;
using Nop.Core.Caching;
using Nop.Core.Data;
using Nop.Core.Domain.Common;
using Nop.Services.Events;

namespace Nop.Services.Common
{
    /// <summary>
    /// Message service
    /// </summary>
    public partial class MessageService : IMessageService
    {
        #region Constants

        /// <summary>
        /// Key for caching
        /// </summary>
        private const string MESSAGES_ALL_KEY = "Nop.message.all";
        /// <summary>
        /// Key for caching
        /// </summary>
        /// <remarks>
        /// {0} : message ID
        /// </remarks>
        private const string MESSAGE_BY_ID_KEY = "Nop.message.id-{0}";
        /// <summary>
        /// Key pattern to clear cache
        /// </summary>
        private const string MESSAGES_PATTERN_KEY = "Nop.message.";
        #endregion
        
        #region Fields
        private readonly IRepository<Message> _messageRepository;
        private readonly IEventPublisher _eventPublisher;
        private readonly ICacheManager _cacheManager;
        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="messageRepository">Message repository</param>
        /// <param name="eventPublisher">Event published</param>
        public MessageService(ICacheManager cacheManager,
            IRepository<Message> messageRepository,
            IEventPublisher eventPublisher)
        {
            this._cacheManager = cacheManager;
            this._messageRepository = messageRepository;
            this._eventPublisher = eventPublisher;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets all Messages
        /// </summary>
        /// <returns>Messages</returns>
        public virtual IList<Message> GetAllMessages()
        {
            string key = MESSAGES_ALL_KEY;
            return _cacheManager.Get(key, () =>
            {
                var query = from a in _messageRepository.Table
                            orderby a.Id
                            select a;
                return query.ToList();
            });
        }

        /// <summary>
        /// Gets a Message 
        /// </summary>
        /// <param name="messageId">Message identifier</param>
        /// <returns>Message</returns>
        public virtual Message GetMessageById(int messageId)
        {
            if (messageId == 0)
                return null;

            string key = string.Format(MESSAGE_BY_ID_KEY, messageId);
            return _cacheManager.Get(key, () => _messageRepository.GetById(messageId));
        }

        /// <summary>
        /// Inserts a Message
        /// </summary>
        /// <param name="message">Message</param>
        public virtual void InsertMessage(Message message)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            _messageRepository.Insert(message);

            _cacheManager.RemoveByPattern(MESSAGES_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(message);
        }

        /// <summary>
        /// Updates the Message
        /// </summary>
        /// <param name="message">Message</param>
        public virtual void UpdateMessage(Message message)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            _messageRepository.Update(message);

            _cacheManager.RemoveByPattern(MESSAGES_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityUpdated(message);
        }

        /// <summary>
        /// Deletes a Message
        /// </summary>
        /// <param name="message">Message</param>
        public virtual void DeleteMessage(Message message)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            _messageRepository.Delete(message);

            _cacheManager.RemoveByPattern(MESSAGES_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityDeleted(message);
        }

        #endregion
    }
}
